﻿using ITSM.Model;
using ITSM.Model.Entity;
using ITSM.Model.Model;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ITSM.Service
{
    public class DemandType :BaseDB, IDemandType
    {

        private readonly ILogger<DemandType> _logger;

        public DemandType(ILogger<DemandType> logger)
        {
            _logger= logger;
        }

        public int AddDemandType(M_DemandType type)
        {
            try
            {
                if (db.Queryable<RYD_DemandType>().Where(res =>res.RYD_Company== type.RYD_Company && res.RYD_IsDelete == 1 && res.RYD_DemandTypeName == type.RYD_DemandTypeName).Count() > 0)
                {
                    return 0;
                }
                RYD_DemandType demandType = new()
                {
                    RYD_Id = Guid.NewGuid().ToString(),
                    RYD_DemandTypeName = type.RYD_DemandTypeName,
                    RYD_IsDelete = 1,
                    RYD_Company = type.RYD_Company,
                    RYD_CreateBy = type.RYD_UserId,
                    RYD_CreateTime = DateTime.Now
                };
                return db.Insertable(demandType).ExecuteReturnEntity().RYD_DemandTypeNo;
            }
            catch (Exception)
            {
                return 0;
            }
            throw new NotImplementedException();
        }

        public bool DeleteDemandType(RYD_DemandTypeLink typeLink)
        {
            var type = db.Queryable<RYD_DemandType>().Where(res => res.RYD_Company == typeLink.RYD_Company && res.RYD_DemandTypeNo == typeLink.RYD_DemandTypeNo).First();
            type.RYD_IsDelete = 2;
            if (db.Updateable(type).UpdateColumns(res => res.RYD_IsDelete).ExecuteCommand() > 0)
            {
                try
                {
                    db.Deleteable<RYD_DemandTypeLink>().Where(res => res.RYD_DemandTypeNo==type.RYD_DemandTypeNo).ExecuteCommand();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            return false;
            throw new NotImplementedException();
        }

        public RYD_DemandType GetDemandType(string RYD_DemandTypeNo)
        {
            throw new NotImplementedException();
        }

        public List<RYD_DemandType> GetDemandTypes(string RYD_Company)
        {
            try
            {
                var list = db.Queryable<RYD_DemandType, RYD_DemandTypeLink, RYD_UserInfo>((type, link, us) => type.RYD_DemandTypeNo == link.RYD_DemandTypeNo && us.RYD_Id == link.RYD_UserId).Where((type, link, us) => (type.RYD_Company == RYD_Company||us.AcrossCompanies==1) && type.RYD_IsDelete == 1 && us.RYD_IsDelete==1).GroupBy((type,link,us)=>new { type.RYD_Id ,type.RYD_DemandTypeNo ,type.RYD_DemandTypeName }).Select((type, link, us)=>new RYD_DemandType{ RYD_Id=type.RYD_Id, RYD_DemandTypeNo=type.RYD_DemandTypeNo, RYD_DemandTypeName=type.RYD_DemandTypeName }).ToList();
                return list;
            }
            catch (Exception e)
            {
                _logger.LogError("获取事件类别错误！{e}", e);
                return new List<RYD_DemandType>();
            }
        }

        public List<RYD_DemandType> QueryDemandType()
        {
            throw new NotImplementedException();
        }

        public List<RYD_DemandType> QueryDemandType(string RYD_Company)
        {
            try
            {
                return db.Queryable<RYD_DemandType>().Where(res => res.RYD_Company == RYD_Company && res.RYD_IsDelete == 1).ToList();
            }
            catch (Exception e)
            {
                _logger.LogError("获取事件类别错误！{e}", e);
                return new List<RYD_DemandType>();
            }
        }

        public List<RYD_DemandType> QueryDemandType(int RYD_IsDelete)
        {
            throw new NotImplementedException();
        }

        public List<RYD_DemandType> QueryDemandType(string RYD_Company, int RYD_IsDelete)
        {
            throw new NotImplementedException();
        }

    }
}
